package com.feng.demo.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

public class HttpClient4 {
	public static void Demo1() throws ClientProtocolException, IOException{
		HttpClient client = new DefaultHttpClient();

		// set proxy
		HttpHost proxy = new HttpHost("127.0.0.1", 8088);
		client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

		client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "user_agent");

		// set authentication
		((DefaultHttpClient) client).getCredentialsProvider().setCredentials(new AuthScope("localhost", 443), new UsernamePasswordCredentials("username", "password"));

		// set formated url
		// List<NameValuePair> qparams = new ArrayList<NameValuePair>();
		// qparams.add(new BasicNameValuePair("q", "httpclient"));
		// qparams.add(new BasicNameValuePair("btnG", "Google Search"));
		// qparams.add(new BasicNameValuePair("aq", "f"));
		// qparams.add(new BasicNameValuePair("oq", null));
		// URI uri = URIUtils.createURI("http", "www.google.com", -1, "/search",
		// URLEncodedUtils.format(qparams, "UTF-8"), null);
		// HttpGet get = new HttpGet(uri);

		HttpPost post = new HttpPost("http://www.baidu.com/");

		// determines the timeout in milliseconds until a connection is
		// established.
		post.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000);
		// defines the socket timeout (SO_TIMEOUT) in milliseconds, which is the
		// timeout for waiting for data or, put differently, a maximum period
		// inactivity between two consecutive data packets).
		post.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 1000);

		// set post method entity
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("IDToken1", "username"));
		nvps.add(new BasicNameValuePair("IDToken2", "password"));
		post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

		HttpResponse response = client.execute(post);
		System.out.println(response.getStatusLine().getStatusCode());
		Header[] headers = response.getAllHeaders();
		HttpEntity entity = response.getEntity();
		System.out.println(entity.getContentType());
		System.out.println(entity.getContentLength());
		System.out.println(EntityUtils.toString(entity));
		entity.consumeContent();
		client.getConnectionManager().shutdown();
	}
	public static void Demo2() throws Exception {
		HttpPost method = new HttpPost("/servlet/SentEmail");
		HttpHost httpHost = new HttpHost("10.100.204.200", 8080);
		HttpClient client = new DefaultHttpClient();
		// set header
		method.addHeader("test", "test");
		String reqXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<request>req</request>";
		System.out.println("Request xml:" + reqXml);
		method.setEntity(new StringEntity(reqXml, "UTF-8"));

		HttpResponse response = client.execute(httpHost, method);
		String resXml = "";
		HttpEntity resEntity = response.getEntity();
		if (resEntity != null) {
			BufferedReader reader = new BufferedReader(new InputStreamReader(resEntity.getContent(), "UTF-8"));
			StringBuffer sb = new StringBuffer();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line);
				sb.append("\r\n");
			}
			resXml = sb.toString();
		}
		//响应结果
		System.out.println("Response content:" + resXml);
	}
	public static void main(String[] args) throws Exception {
		Demo2();
	}
}
